package org.nyt.medicalstore.service.impl;

import org.nyt.medicalstore.model.Drug;
import org.nyt.medicalstore.repository.DrugRepository;
import org.nyt.medicalstore.service.ConsultationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ConsultationServiceImpl implements ConsultationService {
    @Autowired
    private DrugRepository drugRepository;

    // 常见问题库
    private final Map<String, String> faqDatabase;
    
    // 药品使用说明模板
    private final Map<String, String> instructionTemplates;

    public ConsultationServiceImpl() {
        // 初始化FAQ数据库
        faqDatabase = new HashMap<>();
        initializeFAQs();
        
        // 初始化使用说明模板
        instructionTemplates = new HashMap<>();
        initializeInstructionTemplates();
    }

    private void initializeFAQs() {
        faqDatabase.put("营业时间", "我们的营业时间是每天上午9:00至晚上21:00");
        faqDatabase.put("配送方式", "我们提供到店自取和同城配送服务");
        faqDatabase.put("支付方式", "支持现金、微信、支付宝、银行卡等多种支付方式");
        faqDatabase.put("退换政策", "药品一经售出，非质量问题不予退换");
        faqDatabase.put("处方药购买", "购买处方药需要提供医生处方");
    }

    private void initializeInstructionTemplates() {
        instructionTemplates.put("片剂", "口服，一次1-2片，一日3次，饭后服用");
        instructionTemplates.put("胶囊", "口服，一次1-2粒，一日3次，饭后服用");
        instructionTemplates.put("口服液", "口服，一次10ml，一日3次，饭后服用");
        instructionTemplates.put("外用药", "外用，取适量涂抹于患处，一日2-3次");
    }

    @Override
    public Map<String, Object> consult(String query) {
        Map<String, Object> response = new HashMap<>();
        
        // 1. 检查是否是FAQ问题
        String faqAnswer = checkFAQ(query);
        if (faqAnswer != null) {
            response.put("type", "faq");
            response.put("answer", faqAnswer);
            return response;
        }
        
        // 2. 检查是否是药品咨询
        List<Drug> recommendedDrugs = recommendDrugs(query);
        if (!recommendedDrugs.isEmpty()) {
            response.put("type", "drug_recommendation");
            response.put("drugs", recommendedDrugs);
            return response;
        }
        
        // 3. 默认回复
        response.put("type", "general");
        response.put("answer", "抱歉，我无法理解您的问题。请尝试重新描述，或联系我们的客服人员。");
        return response;
    }

    @Override
    public List<Map<String, String>> getFAQs() {
        List<Map<String, String>> faqs = new ArrayList<>();
        for (Map.Entry<String, String> entry : faqDatabase.entrySet()) {
            Map<String, String> faq = new HashMap<>();
            faq.put("question", entry.getKey());
            faq.put("answer", entry.getValue());
            faqs.add(faq);
        }
        return faqs;
    }

    @Override
    public Map<String, String> getDrugInstructions(Long drugId) {
        Optional<Drug> drugOpt = drugRepository.findById(drugId);
        if (!drugOpt.isPresent()) {
            return Collections.emptyMap();
        }
        
        Drug drug = drugOpt.get();
        Map<String, String> instructions = new HashMap<>();
        
        // 获取基本使用说明
        String template = instructionTemplates.get(drug.getDosageForm());
        if (template != null) {
            instructions.put("usage", template);
        }
        
        // 添加药品特定说明
        instructions.put("description", drug.getDescription());
        instructions.put("specification", drug.getSpecification());
        
        return instructions;
    }

    private String checkFAQ(String query) {
        for (Map.Entry<String, String> entry : faqDatabase.entrySet()) {
            if (query.contains(entry.getKey())) {
                return entry.getValue();
            }
        }
        return null;
    }

    private List<Drug> recommendDrugs(String query) {
        // 简单的关键词匹配
        return drugRepository.findByDescriptionContainingOrNameContaining(query, query);
    }
} 